
package Swing;

import common.ReservationUtils;
import entities.Reservation;
import enums.StatusEnum;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;


public class GUI extends javax.swing.JFrame {
  CustomerScreen customerScreen;
  CarScreen carScreen;
  ReservationScreen reservationScreen;
  private final String MODULES_TAB = "Moduły";
  private final String CUSTOMER_TAB = "Klient";
  private final String CAR_TAB = "Samochody";
  private final String RESERVATION_TAB = "Terminy napraw";
  private final String APP_NAME = "Aplikacja bazodanowa wspomagająca zarządzanie serwisem samochodowym";
    
  public GUI() {
    customerScreen = new CustomerScreen();
    carScreen = new CarScreen();
    reservationScreen = new ReservationScreen();
    initComponents();
    setSizeOfMainGui();
    populateMainTree();
    populateGuiTables();
    setTitle(APP_NAME);
   }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TreePanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        MainTree = new javax.swing.JTree();
        MainTabbedPane = new javax.swing.JTabbedPane();
        KlientPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        KlientTable = new javax.swing.JTable();
        CustOptPanel = new javax.swing.JPanel();
        CustOptTxtField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnEditCustomer = new javax.swing.JButton();
        btnDeleteCustomer = new javax.swing.JButton();
        btnRefreshCustData = new javax.swing.JButton();
        btnAddNewCustomer = new javax.swing.JButton();
        btnAddCar = new javax.swing.JButton();
        CarPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        CarTable = new javax.swing.JTable();
        CarOptPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        CarOptTxtField = new javax.swing.JTextField();
        btnEditCar = new javax.swing.JButton();
        btnRefreshCarData = new javax.swing.JButton();
        btnDeleteCar = new javax.swing.JButton();
        btnAddReservation = new javax.swing.JButton();
        ReservationPanel = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        ReservationTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        reservationOptTxtField = new javax.swing.JTextField();
        btnEditReservation = new javax.swing.JButton();
        btnRefreshReservation = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane2.setViewportView(MainTree);

        javax.swing.GroupLayout TreePanelLayout = new javax.swing.GroupLayout(TreePanel);
        TreePanel.setLayout(TreePanelLayout);
        TreePanelLayout.setHorizontalGroup(
            TreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
        );
        TreePanelLayout.setVerticalGroup(
            TreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
        );

        MainTabbedPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                MainTabbedPaneMouseClicked(evt);
            }
        });

        KlientPanel.setPreferredSize(new java.awt.Dimension(700, 300));

        KlientTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(KlientTable);

        CustOptPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Opcje"));

        jLabel2.setText("Wyszukiwarka");

        btnEditCustomer.setText("Edytuj Klienta");
        btnEditCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditCustomerActionPerformed(evt);
            }
        });

        btnDeleteCustomer.setText("Usuń Klienta");
        btnDeleteCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteCustomerActionPerformed(evt);
            }
        });

        btnRefreshCustData.setText("Odśwież dane");
        btnRefreshCustData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshCustDataActionPerformed(evt);
            }
        });

        btnAddNewCustomer.setText("Dodaj Klienta");
        btnAddNewCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNewCustomerActionPerformed(evt);
            }
        });

        btnAddCar.setText("Dodaj samochód");
        btnAddCar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddCarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout CustOptPanelLayout = new javax.swing.GroupLayout(CustOptPanel);
        CustOptPanel.setLayout(CustOptPanelLayout);
        CustOptPanelLayout.setHorizontalGroup(
            CustOptPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CustOptPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CustOptTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(btnAddNewCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(btnEditCustomer)
                .addGap(50, 50, 50)
                .addComponent(btnDeleteCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(btnRefreshCustData, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addComponent(btnAddCar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        CustOptPanelLayout.setVerticalGroup(
            CustOptPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CustOptPanelLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(CustOptPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(CustOptTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddNewCustomer)
                    .addComponent(btnEditCustomer)
                    .addComponent(btnDeleteCustomer)
                    .addComponent(btnRefreshCustData)
                    .addComponent(btnAddCar))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout KlientPanelLayout = new javax.swing.GroupLayout(KlientPanel);
        KlientPanel.setLayout(KlientPanelLayout);
        KlientPanelLayout.setHorizontalGroup(
            KlientPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(KlientPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(KlientPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1077, Short.MAX_VALUE)
                    .addComponent(CustOptPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        KlientPanelLayout.setVerticalGroup(
            KlientPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(KlientPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CustOptPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        MainTabbedPane.addTab("Klient", KlientPanel);

        CarPanel.setPreferredSize(new java.awt.Dimension(700, 300));

        CarTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(CarTable);

        CarOptPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Opcje"));

        jLabel1.setText("Wyszukiwarka:");

        btnEditCar.setText("Edytuj");
        btnEditCar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditCarActionPerformed(evt);
            }
        });

        btnRefreshCarData.setText("Odśwież dane");
        btnRefreshCarData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshCarDataActionPerformed(evt);
            }
        });

        btnDeleteCar.setText("Usuń");
        btnDeleteCar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteCarActionPerformed(evt);
            }
        });

        btnAddReservation.setText("Wprowadź termin naprawy");
        btnAddReservation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddReservationActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout CarOptPanelLayout = new javax.swing.GroupLayout(CarOptPanel);
        CarOptPanel.setLayout(CarOptPanelLayout);
        CarOptPanelLayout.setHorizontalGroup(
            CarOptPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CarOptPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(CarOptTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(69, 69, 69)
                .addComponent(btnEditCar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(70, 70, 70)
                .addComponent(btnDeleteCar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65)
                .addComponent(btnAddReservation, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58)
                .addComponent(btnRefreshCarData, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        CarOptPanelLayout.setVerticalGroup(
            CarOptPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CarOptPanelLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(CarOptPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(CarOptTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditCar)
                    .addComponent(btnRefreshCarData)
                    .addComponent(btnDeleteCar)
                    .addComponent(btnAddReservation))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout CarPanelLayout = new javax.swing.GroupLayout(CarPanel);
        CarPanel.setLayout(CarPanelLayout);
        CarPanelLayout.setHorizontalGroup(
            CarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CarPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(CarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 1077, Short.MAX_VALUE)
                    .addComponent(CarOptPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        CarPanelLayout.setVerticalGroup(
            CarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CarPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CarOptPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        MainTabbedPane.addTab("Samochody", CarPanel);

        ReservationTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(ReservationTable);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Opcje"));

        jLabel3.setText("Wyszukiwarka:");

        btnEditReservation.setText("Edytuj");
        btnEditReservation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditReservationActionPerformed(evt);
            }
        });

        btnRefreshReservation.setText("Odśwież dane");
        btnRefreshReservation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshReservationActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(reservationOptTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(67, 67, 67)
                .addComponent(btnEditReservation, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(75, 75, 75)
                .addComponent(btnRefreshReservation, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(reservationOptTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditReservation)
                    .addComponent(btnRefreshReservation))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout ReservationPanelLayout = new javax.swing.GroupLayout(ReservationPanel);
        ReservationPanel.setLayout(ReservationPanelLayout);
        ReservationPanelLayout.setHorizontalGroup(
            ReservationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ReservationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ReservationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 1077, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        ReservationPanelLayout.setVerticalGroup(
            ReservationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ReservationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        MainTabbedPane.addTab("Terminy napraw", ReservationPanel);

        jMenu1.setText("File");

        jMenuItem1.setText("Zakończ");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(TreePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(MainTabbedPane)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TreePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(MainTabbedPane)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        hibernateUtil.HibernateSessionFactory.closeSessionFactory();
        System.exit(0);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void MainTabbedPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_MainTabbedPaneMouseClicked
        if(evt.getClickCount() == 2 && !evt.isConsumed()){
            Component selected = MainTabbedPane.getSelectedComponent();
            if(selected != null){
                MainTabbedPane.remove(selected);
            }
        }
    }//GEN-LAST:event_MainTabbedPaneMouseClicked

    private void btnAddNewCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNewCustomerActionPerformed
        customerScreen.addNewCustomer(MainTabbedPane);
    }//GEN-LAST:event_btnAddNewCustomerActionPerformed

    private void btnRefreshCustDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshCustDataActionPerformed
        customerScreen.populateKlientTable(KlientTable, KlientPanel, CustOptTxtField);
    }//GEN-LAST:event_btnRefreshCustDataActionPerformed

    private void btnDeleteCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteCustomerActionPerformed
        int id = getCustomerIdFromTable();
        if(id > 0){
            String question = "Czy na pewno chcesz usunąć Klienta o ID = " + id;
            String deleteConfirmation = "Usuwanie danych Klienta";
            int response = JOptionPane.showConfirmDialog(null, question, deleteConfirmation, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(response == JOptionPane.YES_OPTION){
                customerScreen.deleteCustomer(id);
            }
        }
    }//GEN-LAST:event_btnDeleteCustomerActionPerformed

    private void btnEditCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditCustomerActionPerformed
        customerScreen.editCustomerData(MainTabbedPane, getCustomerIdFromTable());
    }//GEN-LAST:event_btnEditCustomerActionPerformed

    private void btnAddCarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddCarActionPerformed
        int id = getCustomerIdFromTable();
        if(id > 0){
           CarsDialog carsDialog = new CarsDialog(this, true, id);
           carsDialog.setVisible(true);
        }
    }//GEN-LAST:event_btnAddCarActionPerformed

    private void btnEditCarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditCarActionPerformed
        carScreen.editCarData(this, MainTabbedPane, getCarIdFromTable());
    }//GEN-LAST:event_btnEditCarActionPerformed

    private void btnRefreshCarDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshCarDataActionPerformed
        carScreen.populateCarTable(CarTable, CarPanel, CarOptTxtField);
    }//GEN-LAST:event_btnRefreshCarDataActionPerformed

    private void btnDeleteCarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteCarActionPerformed
        int id = getCarIdFromTable();
        if(id > 0){
            String question = "Czy na pewno chcesz usunąć Samochód o ID = " + id;
            String deleteConfirmation = "Usuwanie danych Samochodu";
            int response = JOptionPane.showConfirmDialog(null, question, deleteConfirmation, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(response == JOptionPane.YES_OPTION){
                carScreen.deleteCarData(id);
            }
        }
    }//GEN-LAST:event_btnDeleteCarActionPerformed

    private void btnAddReservationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddReservationActionPerformed
        int carId = getCarIdFromTable();
        if(carId > 0){
            ReservationDialog reservationDialog = new ReservationDialog(this, true, carId);
            reservationDialog.setVisible(true);
        }
    }//GEN-LAST:event_btnAddReservationActionPerformed

    private void btnRefreshReservationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshReservationActionPerformed
        reservationScreen.populateReservationTable(ReservationTable, ReservationPanel, reservationOptTxtField);
    }//GEN-LAST:event_btnRefreshReservationActionPerformed

    private void btnEditReservationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditReservationActionPerformed
        int reservationId = getReservationIdFromTable();
        if(reservationId > 0){
            ReservationUtils utils = new ReservationUtils();
            Reservation reservation = utils.getReservationDetails(reservationId);
            if(reservation.getStatus().getCode().equals(StatusEnum.CAN.name())){
                JOptionPane.showMessageDialog(null, "Nie można edytować rezerwacji w statusie Anulowany", "Brak możliwości edycji rezerwacji",JOptionPane.ERROR_MESSAGE);
            } else {
                ReservationDialog reservationDialog = new ReservationDialog(this, reservationId, true);
                reservationDialog.setVisible(true);  
            }
        }
    }//GEN-LAST:event_btnEditReservationActionPerformed

    private int getCustomerIdFromTable(){
        int id = 0;
        try{
            id = (int)KlientTable.getValueAt(KlientTable.getSelectedRow(), 0);
        }
        catch(IndexOutOfBoundsException e){
            JOptionPane.showMessageDialog(null, "Nie wybrano osoby z listy", "Wskaż Klienta z listy do edycji", JOptionPane.ERROR_MESSAGE);
        }      
        return id;
    }
    
    private int getCarIdFromTable(){
        int id = 0;
        try{
            id = (int)CarTable.getValueAt(CarTable.getSelectedRow(), 0);
        }
        catch(IndexOutOfBoundsException e){
            JOptionPane.showMessageDialog(null, "Nie wybrano samochodu z listy", "Wskaż samochód z listy do edycji", JOptionPane.ERROR_MESSAGE);
        }      
        return id;
    }
    
    private int getReservationIdFromTable(){
        int id = 0;
        try{
            id = (int)ReservationTable.getValueAt(ReservationTable.getSelectedRow(), 0);
        }
        catch(IndexOutOfBoundsException e){
            JOptionPane.showMessageDialog(null, "Nie wybrano rezerwacji z listy", "Wskaż rezerwację terminu naprawy z listy do edycji", JOptionPane.ERROR_MESSAGE);
        }      
        return id;
    }    
    
    private void setSizeOfMainGui(){
   //    Dimension d=getMaximumSize(); 
         setSize(1350, 700);
    }
    private void populateMainTree(){
    MainTree.setModel(new DefaultTreeModel(
	new DefaultMutableTreeNode(MODULES_TAB) {
            {
            DefaultMutableTreeNode node_1;
            node_1 = new DefaultMutableTreeNode("Sprzeda\u017C");
            node_1.add(new DefaultMutableTreeNode(CUSTOMER_TAB));
            node_1.add(new DefaultMutableTreeNode(CAR_TAB));
            node_1.add(new DefaultMutableTreeNode(RESERVATION_TAB));
            node_1.add(new DefaultMutableTreeNode("yellow"));
            add(node_1);
            node_1 = new DefaultMutableTreeNode("Magazyn");
            node_1.add(new DefaultMutableTreeNode("basketball"));
            node_1.add(new DefaultMutableTreeNode("soccer"));
            node_1.add(new DefaultMutableTreeNode("football"));
            node_1.add(new DefaultMutableTreeNode("hockey"));
            add(node_1);
            }
	}
    ));
    MainTree.addTreeSelectionListener(new SelectionListener());
    
    
}

    private void populateGuiTables(){
        customerScreen.populateKlientTable(KlientTable, KlientPanel, CustOptTxtField);
        carScreen.populateCarTable(CarTable, CarPanel, CarOptTxtField);
        reservationScreen.populateReservationTable(ReservationTable, ReservationPanel, reservationOptTxtField);
    }

class SelectionListener implements TreeSelectionListener{
    @Override
    public void valueChanged(TreeSelectionEvent e) {
	JTree tree = (JTree) e.getSource();
	DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
	String selectedNodeName = selectedNode.toString();
	if(selectedNode.isLeaf()){
            if( checkIfTabExists(selectedNodeName) ){
                setSelectedTab(selectedNodeName);
            }
            else{
                
                if(CUSTOMER_TAB.equals(selectedNodeName)){
                    MainTabbedPane.add(CUSTOMER_TAB, KlientPanel);
                    setSelectedTab(selectedNodeName);
                }
                if(CAR_TAB.equals(selectedNodeName)){
                    MainTabbedPane.add(CAR_TAB, CarPanel);
                    setSelectedTab(selectedNodeName);
                }    
                if(RESERVATION_TAB.equals(selectedNodeName)){
                    MainTabbedPane.add(RESERVATION_TAB, ReservationPanel);
                    setSelectedTab(selectedNodeName);
                }
            }
	}		
    }
    /**
     * Ustawia główną zakładkę 
     * @param selectedNodeName nazwa zakładki
     */
    private void setSelectedTab(String selectedNodeName){
       MainTabbedPane.setSelectedIndex(MainTabbedPane.indexOfTab(selectedNodeName));        
    }
    
    /**
     * Sprawdza czy podana zakładka jest widoczna
     * Zwraca false jeśli nie istnieje, w przeciwnym razie zwraca true
     * jeśli zakładka jest widoczna na ekranie
     * @param selectedNodeName
     * @return 
     */
    private boolean checkIfTabExists(String selectedNodeName){
        boolean tabExists = false;
        int count = MainTabbedPane.getTabCount();
        for(int i = 0 ; i < count ; i++){
            String label = MainTabbedPane.getTitleAt(i);
            boolean isEnabled = MainTabbedPane.isEnabledAt(i);
            if(selectedNodeName.equals(label) && isEnabled){
                tabExists = true;
            }
        }
        return tabExists;
    }
}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel CarOptPanel;
    private javax.swing.JTextField CarOptTxtField;
    private javax.swing.JPanel CarPanel;
    private javax.swing.JTable CarTable;
    private javax.swing.JPanel CustOptPanel;
    private javax.swing.JTextField CustOptTxtField;
    private javax.swing.JPanel KlientPanel;
    private javax.swing.JTable KlientTable;
    private javax.swing.JTabbedPane MainTabbedPane;
    private javax.swing.JTree MainTree;
    private javax.swing.JPanel ReservationPanel;
    private javax.swing.JTable ReservationTable;
    private javax.swing.JPanel TreePanel;
    private javax.swing.JButton btnAddCar;
    private javax.swing.JButton btnAddNewCustomer;
    private javax.swing.JButton btnAddReservation;
    private javax.swing.JButton btnDeleteCar;
    private javax.swing.JButton btnDeleteCustomer;
    private javax.swing.JButton btnEditCar;
    private javax.swing.JButton btnEditCustomer;
    private javax.swing.JButton btnEditReservation;
    private javax.swing.JButton btnRefreshCarData;
    private javax.swing.JButton btnRefreshCustData;
    private javax.swing.JButton btnRefreshReservation;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextField reservationOptTxtField;
    // End of variables declaration//GEN-END:variables

}


